/**
 * 
 */
package com.slink.fxfk.common.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.slink.fxfk.framework.util.CustomModelAndView;
import com.slink.fxfk.framework.util.StringUtil;

/**
 * @author 新
 *
 */
@Controller
@RequestMapping("/file")
public class UpLoadAction {

	@RequestMapping("/upload")
	public CustomModelAndView upload(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
				request.getServletContext());
		if (multipartResolver.isMultipart(request)) {
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			Iterator<String> iterator = multiRequest.getFileNames();
			while (iterator.hasNext()) {
				MultipartFile file = multiRequest.getFile((String) iterator.next());
				if (null != file && file.getSize()!=0 && StringUtil.isNotBlank(file.getName())) {
					String fileName = file.getOriginalFilename();
					String path1 = Thread.currentThread()
							.getContextClassLoader().getResource("").getPath()
							+ "resourcefiles" + File.separator;
					File fileDir = new File(path1);
					if (!fileDir.exists()) {
						fileDir.mkdir();
					}
					// 下面的加的日期是为了防止上传的名字一样
					String path2 = path1
							+ new SimpleDateFormat("yyyyMMddHHmmss")
									.format(new Date()) + fileName;
					File localFile = new File(path2);
					file.transferTo(localFile);
				}
			}
		}

		return new CustomModelAndView("/uploadsuccess");
	}
	
	@SuppressWarnings("resource")
	@RequestMapping("/download")
	public CustomModelAndView download(String fileName, HttpServletRequest request,
            HttpServletResponse response){
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		response.setHeader("Content-Disposition", "attachment;fileName="
                + fileName);
		try {
			String path = Thread.currentThread()
					.getContextClassLoader().getResource("").getPath()
					+ "resourcefiles" + File.separator;
			InputStream inputStream = new FileInputStream(new File(path
                    + File.separator + fileName));
			OutputStream os = response.getOutputStream();
            byte[] b = new byte[2048];
            int length=-1;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
            }
             // 这里主要关闭。
            os.close();
		} catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
		
		return null;
	}
	
	
	
	
	
	
	
	
	
	

}
